/*
 desc web add this script.
 <script id="ats" type="text/javascript" src="tracing.js" data="a=b&c=d"></script>
 */

function do_post(params) {
    var url = "http://localhost:9090/tas/trace";
    try {
        jQuery.ajax({
                    type : "GET",
                    url : url,
                    data : params,
                    dataType : 'jsonp',
                    jsonp : 'jsonpcallback',
                    success : function(responseText, textStatus, XMLHttpRequest) {
                        //alert("succ:" + responseText);
                    }
                });
    } catch(err) {
        //alert(err);
    }

}
function do_cookie() {
    var cookieString = new String(document.cookie);
    var cookieHeader = "pc_tas";
    var beginPosition = cookieString.indexOf(cookieHeader);
    if (beginPosition != -1) //Get Cookie
    {
        var arrCookie = cookieString.split(";");
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (arr[0].replace(/(^\s*)|(\s*$)/gi, "") == "pc_tas")return arr[1];
        }
    }
    else //add Cookie
    {
        var exp = new Date();
        var value = exp.getTime();
        var Days = 30; //此 cookie 将被保存 30 天
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = "pc_tas=" + value + ";expires=" + exp.toGMTString();
        return value;
    }
}

$(function() {
    var pageId = document.getElementById('ats').getAttribute('data');
    var clientId = do_cookie();
    var referrer = document.referrer;
    alert(window.location.href);
    alert(window.location.pathname);
    alert(window.location.search);
    var rr = encodeURIComponent(encodeURIComponent(referrer));
    var pdata = pageId + "&rr=" + rr + "&clientId=" + clientId;
    do_post(pdata);
});

